package com.yxcz.controller;

import java.util.ArrayList;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yxcz.model.AddDeptForm;
import com.yxcz.model.AddMenuForm;
import com.yxcz.model.AddRoleForm;
import com.yxcz.model.Dept;
import com.yxcz.model.JsonDept;
import com.yxcz.model.JsonMenu;
import com.yxcz.model.Menu;
import com.yxcz.model.Role;
import com.yxcz.model.UpdateDeptForm;
import com.yxcz.model.UpdateMenuForm;
import com.yxcz.model.UpdateRoleForm;
import com.yxcz.model.User;
import com.yxcz.service.DeptService;

@Controller
@RequestMapping("/Dept")
public class DeptController {

	@Resource
	private DeptService deptService;

	/**
	 * 部门管理页面
	 * 
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value = "index")
	public ModelAndView index(ModelAndView modelAndView) {

		// 传送部门树JSON到前台
		modelAndView.addObject("depts", deptService.jsonList());
		modelAndView.setViewName("Manager/Dept");
		return modelAndView;
	}

	/**
	 * AJAX获取部门信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "getDept")
	@ResponseBody
	public Dept getDept(Integer id) {
		if (id == null)
			return null;
		return deptService.find(id);
	}

	/**
	 * 删除部门
	 * 
	 * @param id
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value = "delete")
	public ModelAndView delete(Integer id, ModelAndView modelAndView) {

		if (id == null) {
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}

		deptService.delete(id);
		modelAndView.addObject("msg", "删除");
		modelAndView.setViewName("Common/success");

		return modelAndView;

	}

	@RequestMapping(value = "update")
	public ModelAndView update(@Validated UpdateDeptForm form, BindingResult bindingResult, ModelAndView modelAndView) {

		if (bindingResult.hasErrors()) {
			modelAndView.setViewName("redirect:/Common/500Page");
		} else {
			deptService.update(form);
			modelAndView.addObject("msg", "修改");
			modelAndView.setViewName("Common/success");
		}
		return modelAndView;

	}

	@RequestMapping(value = "add")
	public ModelAndView add(@Validated AddDeptForm form, BindingResult bindingResult, ModelAndView modelAndView) {
		if (bindingResult.hasErrors()) {
			modelAndView.setViewName("redirect:/Common/500Page");
		} else {
			try {
				deptService.add(form);
				modelAndView.addObject("msg", "增加");
				modelAndView.setViewName("Common/success");
			} catch (Exception e) {
				modelAndView.setViewName("redirect:/Common/500Page");
			}

		}

		return modelAndView;

	}
	
	/**
	 * 选择部门菜单
	 * 
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value = "select")
	public ModelAndView select(ModelAndView modelAndView,String formname) {

		// 传送部门树JSON到前台
		modelAndView.addObject("depts", deptService.jsonList());
		modelAndView.addObject("formname",formname);
		modelAndView.setViewName("Common/deptSelect");
		return modelAndView;
	}

}
